<template>
  <div>
    <a-card :bordered="false" :bodyStyle="tstyle">
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="12" :sm="24">
              <a-form-item label="名称">
                <a-input v-model="queryParam.name" allow-clear placeholder="请输入名称" />
              </a-form-item>
            </a-col>
            <a-col :md="12" :sm="24">
              <span class="table-page-search-submitButtons">
                <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
                <a-button style="margin-left: 8px" @click="() => (queryParam = {})">重置</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </a-card>
    <a-card :bordered="false">
      <s-table
        ref="table"
        :columns="columns"
        :data="loadData"
        :alert="options.alert"
        :rowKey="(record) => record.id"
        :rowSelection="options.rowSelection"
      >
        <template class="table-operator" slot="operator">
          <a-button type="primary" icon="plus" @click="$refs.detail.open()">新增作业标签</a-button>

          <a-popconfirm placement="topRight" title="确认删除？" @confirm="() => batchDelete()">
            <a-button type="danger" :disabled="selectedRowKeys.length < 1"> <a-icon type="delete" />批量删除</a-button>
          </a-popconfirm>
        </template>
        <span slot="status" slot-scope="text, record">
          <a-switch
            :loading="swtichLoading"
            checked-children="正常"
            un-checked-children="停用"
            :defaultChecked="text === 0"
            :disabled="record.status == 2"
            @click="() => editHomeworkTagStatus(text, record)"
          />
        </span>
        <span slot="action" slot-scope="text, record" v-if="record.status != 2">
          <a @click="$refs.detail.open(record)">编辑</a>
          <a-divider type="vertical" />
          <a-popconfirm placement="topRight" title="确认删除？" @confirm="() => singleDelete(record)">
            <a>删除</a>
          </a-popconfirm>
        </span>
      </s-table>
      <homework-tag-detail ref="detail" @ok="handleOk" />
    </a-card>
  </div>
</template>
<script>
import { STable, XDown } from '@/components'
import { homeworkTagPage, homeworkTagDelete, homeworkTagChangeStatus } from '@/api/modular/system/homeworkTagManage'
import HomeworkTagDetail from './detail.vue'
export default {
  components: {
    STable,
    XDown,
    HomeworkTagDetail
  },
  data() {
    return {
      // 查询参数
      queryParam: {
        status: 0
      },
      swtichLoading: false,
      // 表头
      columns: [
        {
          title: '名称',
          align: 'center',
          dataIndex: 'name'
        }
      ],
      tstyle: { 'padding-bottom': '0px', 'margin-bottom': '10px' },
      // 加载数据方法 必须为 Promise 对象
      loadData: (parameter) => {
        return homeworkTagPage(Object.assign(parameter, this.queryParam)).then((res) => {
          return res.data
        })
      },
      selectedRowKeys: [],
      selectedRows: [],
      options: {
        alert: {
          show: true,
          clear: () => {
            this.selectedRowKeys = []
          }
        },
        rowSelection: {
          selectedRowKeys: this.selectedRowKeys,
          onChange: this.onSelectChange
        }
      }
    }
  },
  created() {
    this.columns.push({
      title: '操作',
      width: '150px',
      dataIndex: 'action',
      scopedSlots: { customRender: 'action' }
    })
  },
  methods: {
    onChange(e) {
      this.handleOk()
    },

    /**
     * 修改项目状态
     */
    editHomeworkTagStatus(code, record) {
      const status = code == 0 ? 1 : 0
      const action = code == 0 ? '停用' : '启用'
      this.swtichLoading = true
      homeworkTagChangeStatus({ id: record.id, status: status })
        .then((res) => {
          if (res.success) {
            this.$message.success(action + '操作成功')
            this.$refs.table.refresh()
          } else {
            this.$message.error(action + '操作失败：' + res.message)
          }
        })
        .finally(() => {
          this.swtichLoading = false
          this.$refs.table.refresh()
        })
    },

    /**
     * 单个删除
     */
    singleDelete(record) {
      const param = [{ id: record.id }]
      this.homeworkTagDelete(param)
    },
    /**
     * 批量删除
     */
    batchDelete() {
      debugger
      const paramIds = this.selectedRowKeys.map((d) => {
        return { id: d }
      })
      this.homeworkTagDelete(paramIds)
    },
    homeworkTagDelete(record) {
      homeworkTagDelete(record).then((res) => {
        if (res.success) {
          this.$message.success('删除成功')
          this.$refs.table.clearRefreshSelected()
        } else {
          this.$message.error('删除失败') // + res.message
        }
      })
    },
    handleOk() {
      this.$refs.table.refresh()
    },
    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    }
  }
}
</script>
<style lang="less">
.table-operator {
  margin-bottom: 18px;
}
button {
  margin-right: 8px;
}
</style>
